#include "simple_db.h"
#include <fcntl.h>


#define NHASH_DEF   137     // default hash table size
#define PTR_SZ      6       // size of pter field hash chain
#define HASH_OFF    PTR_SZ  // hash table offset in index file

typedef unsigned long DBHASH;

typedef struct {
    int idxfd;
    int datfd;
    char* name;
    DBHASH nhash;
    off_t hashoff; 
} DB;


/*
 * internal function prototype
 */
static err_dump(const char* err_msg);
static DB* _db_alloc();

/*
 * internal function implement
 */
static err_dump(const char* err_msg) {
    printf("%s\n", err_msg);
    exit(1);
}

static DB* _db_alloc(int namelen) {
    DB* db;
    if ((db = calloc(1, sizeof(DB))) == NULL)
        err_dump("_db_alloc: calloc error for DB");
    /*
     * +5 for ".idx" or ".dat" plus null at end
     */
    if ((db->name == malloc(namelen + 5)) == NULL)
        err_dump("_db_alloc: malloc error for name");
    return db;
}

DBHANDLE db_open(const char* path, int oflag, ...) {
    int len = strlen(path);
    DB* db;
    if ((db = _db_alloc(len)) == NULL)
        err_dump("db_open: _db_alloc error for DB");

    db->nhash = NHASH_DEF;

}